﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Top.Api;
using Top.Api.Request;
using Top.Api.Response;

namespace ZGEDrySaltery.Api
{
    public class TbkApiHelper
    {
        private static string appkey = "24752712";
        private static string secret = "98a8a524dcabd50932ac1857d624e328";
        private static string pid = "mm_119561674_41552973_213758623";
        private static long adzone_id = 213758623L; //mm_119561674_41552973_213758623 

        //private static string appkey = "23575091";//
        //private static string secret = "703b900532eda324161be4f671d89689";//
        //private static long adzone_id = 143480706L;//mm_xxx_xxx_xxx的第三位 	mm_27924908_20044382_143480706 淘抢购列表api
        private static string userid = "644088245";
        private static string url = "http://gw.api.taobao.com/router/rest";
        
        #region 生成淘口令===================================
        public static string GetTkl(string tuiurl, string tuurl, string title = "粉丝福利购,超值活动,惊喜活动多多")
        {
            #region 调用接口生成淘口令
            ITopClient client = new DefaultTopClient(url, appkey, secret);
            TbkTpwdCreateRequest req = new TbkTpwdCreateRequest();
            req.UserId = userid;
            if (title.Length < 5)
            {
                title = title + "-粉丝福利购";
            }
            req.Text = title;
            req.Url = tuiurl;
            req.Logo = tuurl;
            req.Ext = "{}";
            string msg = "";
            try
            {
                TbkTpwdCreateResponse rsp = client.Execute(req);
                msg = "复制这条信息，打开[手机淘宝]" + rsp.Data.Model + "立享优惠";
            }
            catch (Exception ex)
            {
                msg = "复制这条信息，打开[手机淘宝]立享优惠";
            }
            #endregion
            return msg;
        }
        #endregion

        #region 生成短链接===================================
        public static string GetDuanUrl(string tuiurl)
        {
            #region 调用接口生成短网址
            ITopClient client = new DefaultTopClient(url, appkey, secret);
            TbkSpreadGetRequest req = new TbkSpreadGetRequest();
            List<TbkSpreadGetRequest.TbkSpreadRequestDomain> list2 = new List<TbkSpreadGetRequest.TbkSpreadRequestDomain>();
            TbkSpreadGetRequest.TbkSpreadRequestDomain obj3 = new TbkSpreadGetRequest.TbkSpreadRequestDomain();
            list2.Add(obj3);
            obj3.Url = tuiurl;
            req.Requests_ = list2;
            try
            {
                TbkSpreadGetResponse rsp = client.Execute(req);
                if (rsp.Results != null && rsp.Results.Count > 0)
                {
                    return rsp.Results[0].Content;
                }
                else
                {
                    return "";
                }
            }
            catch (Exception ex)
            {
                return "";
            }

            #endregion
        }
        #endregion

        #region 好券清单API【导购】列表======================
        /// <summary>
        /// 好券清单API【导购】列表
        /// </summary>
        /// <param name="platform">1：PC，2：无线，默认：1</param>
        /// <param name="cat">后台类目ID，用,分割，最大10个，该ID可以通过taobao.itemcats.get接口获取到</param>
        /// <param name="page_size">页大小，默认5，1~100</param>
        /// <param name="q">查询词</param>
        /// <param name="page_no">第几页，默认：1（当后台类目和查询词均不指定的时候，最多出10000个结果</param>
        /// <returns></returns>
        public static TbkDgItemCouponGetResponse get_dg_coupon_list(int platform = 1, string cat = null, int page_size = 20, string q = null, int page_no = 1)
        {
            //string userip = DTRequest.GetIP();
            ITopClient client = new DefaultTopClient(url, appkey, secret, Constants.FORMAT_JSON);
            TbkDgItemCouponGetRequest req = new TbkDgItemCouponGetRequest();
            req.AdzoneId = adzone_id;
            req.Platform = platform;
            if (!string.IsNullOrEmpty(cat))
            {
                req.Cat = cat;
            }
            req.PageSize = page_size;
            if (!string.IsNullOrEmpty(q))
            {
                req.Q = q;
            }
            req.PageNo = page_no;
            TbkDgItemCouponGetResponse rsp = client.Execute(req);
            return rsp;
        }
        #endregion

        #region 淘宝客商品查询===============================
        public static TbkItemGetResponse get_tbk_item(string q, int page_size = 20, int page_no = 1,int platform=1)
        {
            ITopClient client = new DefaultTopClient(url, appkey, secret);
            TbkItemGetRequest req = new TbkItemGetRequest();
            req.Fields = "num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick";

            //req.Fields = "num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick,cat_leaf_name,cat_name,click_url,commission_rate,coupon_amount,coupon_price,coupon_start_fee,shop_title,tk_rate,zk_final_price_wap";
            req.Q = q;//查询词
           // req.Cat = null;//后台类目ID，用,分割，最大10个，该ID可以通过taobao.itemcats.get接口获取到
            //req.Itemloc = "杭州";//所在地
            req.Sort = "total_sales_des";
            //req.IsTmall = false;//是否商城商品，设置为true表示该商品是属于淘宝商城商品，设置为false或不设置表示不判断这个属性
            //req.IsOverseas = false;//是否海外商品，设置为true表示该商品是属于海外商品，设置为false或不设置表示不判断这个属性
            //req.StartPrice = 10L;//折扣价范围下限，单位：元
            //req.EndPrice = 10L;//折扣价范围上限，单位：元
            //req.StartTkRate = 123L;//淘客佣金比率上限，如：1234表示12.34%
            //req.EndTkRate = 123L;//淘客佣金比率下限，如：1234表示12.34%
            req.Platform = platform;//链接形式：1：PC，2：无线，默认：１
            req.PageNo = page_no;//第几页，默认：１
            req.PageSize = page_size;//页大小
            TbkItemGetResponse rsp = client.Execute(req);
            //if (rsp != null && rsp.Results != null && rsp.Results.Count > 0)
            //{
            //    Top.Api.Domain.NTbkItem item= rsp.Results[0];
                
            //    TbkItemConvertRequest req2 = new TbkItemConvertRequest();
            //    req2.Fields = "num_iid,click_url";
            //    req2.NumIids = "123,456";
            //    req2.AdzoneId = 123L;
            //    req2.Platform = platform;
            //    req2.Unid = "1";
            //    req2.Dx = "1";
            //    TbkItemConvertResponse rsp2 = client.Execute(req);
            //    Console.WriteLine(rsp.Body);
            //}
            return rsp;
        }
        #endregion

        #region 聚划算商品搜索接口
        /// <summary>
        /// 聚划算商品搜索接口
        /// </summary>
        /// <param name="cat">后台类目ID，用,分割，最大10个，该ID可以通过taobao.itemcats.get接口获取到</param>
        /// <param name="page_size">页大小，默认5，1~100</param>
        /// <param name="q">查询词</param>
        /// <param name="page_no">第几页，默认：1（当后台类目和查询词均不指定的时候，最多出10000个结果</param>
        /// <returns></returns>
        public static JuItemsSearchResponse ju_items_searchn_list(long cat = 0, int page_size = 20, string q = null, int page_no = 1)
        {
            ITopClient client = new DefaultTopClient(url, appkey, secret,Constants.FORMAT_JSON);
            JuItemsSearchRequest req = new JuItemsSearchRequest();
            JuItemsSearchRequest.TopItemQueryDomain obj1 = new JuItemsSearchRequest.TopItemQueryDomain();
            obj1.CurrentPage = page_no;
            obj1.PageSize = page_size;
            obj1.Pid = pid; //媒体pid,必传
            obj1.Postage = true;//是否包邮,可不传
            obj1.Status = 2L;//状态，预热：1，正在进行中：2,可不传
            if (cat != 0)
            {
                obj1.TaobaoCategoryId = cat;//淘宝类目id,可不传
            }
            if (!string.IsNullOrEmpty(q))
            {
                obj1.Word = q;//搜索关键词,可不传
            }

            req.ParamTopItemQuery_ = obj1;
            JuItemsSearchResponse rsp = client.Execute(req);
            return rsp;
        }
        #endregion
    }
}
